package com.shambles.manage2.tkmapper.mapper;

import com.shambles.manage2.tkmapper.entity.CostDetail;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.additional.insert.InsertListMapper;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;
import java.util.Map;

public interface CostDetailMapper extends Mapper<CostDetail>, InsertListMapper<CostDetail> {

    /**
     * 根据费用ID 查询明细数据
     */
    @Select("select * from cost_detail where total_guid='${guid}'")
    List<CostDetail> queryCostDetail(@Param("guid") String guid);

    @Select("SELECT cattle_guid, FROM_UNIXTIME(LEFT(time, 10),'%Y-%m-%d') nTime,sum(cost_detail.${field}) number FROM cost_detail INNER JOIN cost ON time>${sTime} AND time<${eTime} AND cost_detail.cattle_guid = '${cattleGuid}' AND cost.guid = cost_detail.total_guid GROUP BY nTime")
    List<Map<String, Object>> countNumByCattleGuid(@Param("field")String field, @Param("cattleGuid") String cattleGuid, @Param("sTime") Long sTime,@Param("eTime") Long eTime);

    @Select("SELECT merchant_guid,merchant_name,pay_type,sum(cost_detail.number) number,sum(cost_detail.quarantine_number) quarantine,sum(cost_detail.number - cost_detail.quarantine_number) unQuarantine,sum(cost_detail.money) money  FROM cost LEFT JOIN cost_detail ON time>${sTime} AND time<${eTime} AND cost.guid = cost_detail.total_guid GROUP BY merchant_guid")
    List<Map<String, Object>> countByUser(@Param("sTime") Long sTime,@Param("eTime") Long eTime);

    @Select("SELECT cattle_guid,type,sum(cost_detail.number) number,sum(cost_detail.quarantine_number) quarantine,sum(cost_detail.number - cost_detail.quarantine_number) unQuarantine,sum(cost_detail.money) money  FROM cost INNER JOIN cost_detail ON  merchant_guid = '${userGuid}' AND cost.guid = cost_detail.total_guid GROUP BY cattle_guid")
    List<Map<String, Object>> countByUserDetail(@Param("userGuid")String userGuid, @Param("sTime") Long sTime,@Param("eTime") Long eTime);
    @Select("SELECT merchant_guid,merchant_name,cattle_guid,type,pay_type,sum(cost_detail.number) number,sum(cost_detail.quarantine_number) quarantine,sum(cost_detail.number - cost_detail.quarantine_number) unQuarantine,sum(cost_detail.money) money,time,admission_time  FROM cost INNER JOIN cost_detail ON  cost.guid = cost_detail.total_guid INNER JOIN admission_register b on admission_guid=b.guid GROUP BY merchant_guid,cattle_guid")
    List<Map<String, Object>> countByUsersDetail(@Param("sTime") Long sTime,@Param("eTime") Long eTime);

    @Select("SELECT cattle_guid, FROM_UNIXTIME(LEFT(time, 10),'%Y-%m-%d') nTime,sum(cost_detail.number) number,sum(cost_detail.quarantine_number)*100/sum(cost_detail.number) rate FROM cost_detail INNER JOIN cost ON time>${sTime} AND time<${eTime} AND cost_detail.cattle_guid = '${cattleGuid}' AND cost.guid = cost_detail.total_guid GROUP BY nTime")
    List<Map<String, Object>> countRateByCattleGuid(@Param("cattleGuid") String cattleGuid, @Param("sTime") Long sTime,@Param("eTime") Long eTime);

    List<CostDetail> findByGuid(String guid);
}